package com.lee.mysql.controller;

import com.lee.mysql.service.TableDataSyncService;
import com.lee.mysql.service.TableSyncService;
import com.lee.mysql.util.DecryptUtil;
import com.lee.mysql.util.JdbcUtil;
import com.lee.mysql.vo.MenuVo;
import com.lee.mysql.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/mysql")
@Api(tags = {"Mysql表结构同步"})
public class MysqlController {
    @Autowired
    private TableSyncService tableSyncService;
    @Autowired
    private TableDataSyncService tableDataSyncService;


    // 新表结构同步
    @RequiresPermissions("system:tabSync:one")
    @RequestMapping(value = "/syncNewTable", method = RequestMethod.POST)
    @ApiOperation(value = "新表结构同步")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query"),
         @ApiImplicitParam(name = "tableName",value = "数据库表名",paramType = "query")
    })
    public Result<String> syncNewTable(String configId,String tableName) {
        if (StringUtils.isBlank(configId) || StringUtils.isBlank(tableName)){
            return new Result<>(Result.FAIL,"请选择配置和表");
        }

        List<String> tableList = Arrays.asList(tableName.split(","));

        int i = tableSyncService.syncSomeNewTable(configId,tableList);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"操作成功");
        }else {
            return new Result<>(Result.FAIL,"操作失败");
        }
    }

    // 新表(多表)结构同步
    @RequiresPermissions("system:tabSync:some")
    @RequestMapping(value = "/syncSomeNewTable", method = RequestMethod.POST)
    @ApiOperation(value = "新表(多表)结构同步")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query"),
         @ApiImplicitParam(name = "tableName",value = "数据库表名(多个逗号隔开)",paramType = "query")
    })
    public Result<String> syncSomeNewTable(String configId,String tableName) {
        if (StringUtils.isBlank(configId) || StringUtils.isBlank(tableName)){
            return new Result<>(Result.FAIL,"请选择配置和表");
        }

        List<String> tableList = Arrays.asList(tableName.split(","));

        int i = tableSyncService.syncSomeNewTable(configId,tableList);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"同步表数:"+tableList.size()+" 成功表数:"+i);
        }else {
            return new Result<>(Result.FAIL,"操作失败");
        }
    }

    // 单表(旧表)结构同步
    @RequiresPermissions("system:tabSync:one")
    @RequestMapping(value = "/syncOldTable", method = RequestMethod.POST)
    @ApiOperation(value = "单表(旧表)结构同步")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query"),
        @ApiImplicitParam(name = "tableName",value = "数据库表名(单个/多个逗号隔开)",paramType = "query")
    })
    public Result<String> syncOldTable(String configId,String tableName) {
        if (StringUtils.isBlank(configId) || StringUtils.isBlank(tableName)){
            return new Result<>(Result.FAIL,"请选择配置和表");
        }

        List<String> tableList = Arrays.asList(tableName.split(","));

        int i = tableSyncService.syncOldTable(configId,tableList);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"操作成功");
        }else {
            return new Result<>(Result.FAIL,"操作失败");
        }
    }


    // 多表(旧表)结构同步
    @RequiresPermissions("system:tabSync:some")
    @RequestMapping(value = "/syncSomeOldTable", method = RequestMethod.POST)
    @ApiOperation(value = "多表(旧表)结构同步")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query"),
        @ApiImplicitParam(name = "tableName",value = "数据库表名(单个/多个逗号隔开)",paramType = "query")
    })
    public Result<String> syncSomeOldTable(String configId,String tableName) {
        if (StringUtils.isBlank(configId) || StringUtils.isBlank(tableName)){
            return new Result<>(Result.FAIL,"请选择配置和表");
        }

        List<String> tableList = Arrays.asList(tableName.split(","));

        int i = tableSyncService.syncOldTable(configId,tableList);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"操作成功");
        }else {
            return new Result<>(Result.FAIL,"操作失败");
        }
    }

    // 全表结构同步
    @RequiresPermissions("system:tabSync:all")
    @RequestMapping(value = "/syncAllTable", method = RequestMethod.POST)
    @ApiOperation(value = "全表结构同步")
    @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query")
    public Result<String> syncAllTable(String configId) {
        if (StringUtils.isBlank(configId)){
            return new Result<>(Result.FAIL,"请选择配置");
        }

        int i = tableSyncService.syncAllTable(configId);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"操作成功");
        }else {
            return new Result<>(Result.FAIL,"操作失败");
        }
    }



    // 数据同步
    @RequiresPermissions("system:dataSync:doSync")
    @RequestMapping(value = "/syncTabData", method = RequestMethod.POST)
    @ApiOperation(value = "数据同步")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query"),
            @ApiImplicitParam(name = "tableName",value = "数据库表名(单个/多个逗号隔开)",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每次读写条数",paramType = "query")
    })
    public Result<String> syncTabData(String configId,String tableName,Integer size) {
        if (StringUtils.isBlank(configId) || StringUtils.isBlank(tableName)){
            return new Result<>(Result.FAIL,"请选择配置和表");
        }

        List<String> tableList = Arrays.asList(tableName.split(","));

        int i = tableDataSyncService.syncTableData(configId,tableList,size);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"数据同步表数量:"+tableList.size()+" 同步成功:"+i);
        }else {
            return new Result<>(Result.FAIL,"操作失败");
        }
    }



    // 函数同步
    @RequiresPermissions("system:funSync:doSync")
    @RequestMapping(value = "/syncFunData", method = RequestMethod.POST)
    @ApiOperation(value = "函数同步")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query"),
            @ApiImplicitParam(name = "funName",value = "函数名(单个/多个逗号隔开)",paramType = "query")
    })
    public Result<String> syncFunData(String configId,String funName) {
        if (StringUtils.isBlank(configId) || StringUtils.isBlank(funName)){
            return new Result<>(Result.FAIL,"请选择配置和函数");
        }

        List<String> funList = Arrays.asList(funName.split(","));

        int i = tableSyncService.syncFunction(configId,funList);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"同步成功!");
        }else {
            return new Result<>(Result.FAIL,"操作失败");
        }
    }



    // 视图同步
    @RequiresPermissions("system:viewSync:doSync")
    @RequestMapping(value = "/syncViewData", method = RequestMethod.POST)
    @ApiOperation(value = "视图同步")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query"),
            @ApiImplicitParam(name = "viewName",value = "视图名(单个/多个逗号隔开)",paramType = "query")
    })
    public Result<String> syncViewData(String configId,String viewName) {
        if (StringUtils.isBlank(configId) || StringUtils.isBlank(viewName)){
            return new Result<>(Result.FAIL,"请选择配置和视图");
        }

        List<String> viewList = Arrays.asList(viewName.split(","));

        int i = tableSyncService.syncView(configId,viewList);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"同步成功!");
        }else {
            return new Result<>(Result.FAIL,"操作失败");
        }
    }



    // 测试数据库连接
    @RequestMapping(value = "/testDbConn", method = RequestMethod.POST)
    @ApiOperation(value = "测试数据库连接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "host",value = "IP:端口",paramType = "query"),
            @ApiImplicitParam(name = "db",value = "数据库表名",paramType = "query"),
            @ApiImplicitParam(name = "user",value = "账号",paramType = "query"),
            @ApiImplicitParam(name = "pwd",value = "密码",paramType = "query"),
            @ApiImplicitParam(name = "ce",value = "字符集",paramType = "query")
    })
    public Result<String> testDbConn(String host,String db,String user,String pwd,String ce) {
        String b64 = DecryptUtil.deCodeB64(pwd);
        boolean flag = JdbcUtil.testDbConn(host, db, user, b64, ce);

        if (flag){
            return new Result<>(Result.SUCCESS,"测试连接成功");
        }else {
            return new Result<>(Result.FAIL,"测试连接失败");
        }
    }



    // 获取源数据库所有表
    @RequestMapping(value = "/getMasterTabList", method = RequestMethod.GET)
    @ApiOperation(value = "获取源数据库所有表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query")
    })
    //public Result<List<String>> getMasterTabList(String configId) {
    public Result<List<MenuVo>> getMasterTabList(String configId) {
        //List<String> list = tableSyncService.getMasterTabList(configId);

        List<MenuVo> list = new ArrayList<>();
        List<String> list1 = tableSyncService.getMasterTabList(configId);
        int id = -1;
        for (String item : list1) {
            id++;
            MenuVo vo = new MenuVo();
            vo.setTitle(item);
            vo.setId(""+id);
            list.add(vo);
        }

        return new Result<>(Result.SUCCESS,"操作成功",list);
    }



    // 获取源数据库所有函数
    @RequestMapping(value = "/getMasterFunList", method = RequestMethod.GET)
    @ApiOperation(value = "获取源数据库所有函数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query")
    })
    public Result<List<MenuVo>> getMasterFunList(String configId) {

        List<MenuVo> list = new ArrayList<>();
        List<String> list1 = tableSyncService.queryAllFunction(configId);
        int id = -1;
        for (String item : list1) {
            id++;
            MenuVo vo = new MenuVo();
            vo.setTitle(item);
            vo.setId(""+id);
            list.add(vo);
        }

        return new Result<>(Result.SUCCESS,"操作成功",list);
    }



    // 获取源数据库所有视图
    @RequestMapping(value = "/getMasterViewList", method = RequestMethod.GET)
    @ApiOperation(value = "获取源数据库所有视图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId",value = "配置ID",paramType = "query")
    })
    public Result<List<MenuVo>> getMasterViewList(String configId) {

        List<MenuVo> list = new ArrayList<>();
        List<String> list1 = tableSyncService.queryAllView(configId);
        int id = -1;
        for (String item : list1) {
            id++;
            MenuVo vo = new MenuVo();
            vo.setTitle(item);
            vo.setId(""+id);
            list.add(vo);
        }

        return new Result<>(Result.SUCCESS,"操作成功",list);
    }



}
